<template>
  <div class="hm-article">
    <div
      class="item"
      v-for="item in list"
      :key="item.id"
      @click="$router.push({ path: '/detail/' + item.id })"
    >
      <div class="top">
        <!-- <div class="left">1</div> -->
        <img :src="item.avatar" alt="" />
        <div class="right">
          <div class="title">{{ item.stem }}</div>
          <div class="username">
            <span>{{ item.creator }}</span>
            <span>|</span>
            <span>{{ item.createdAt }}</span>
          </div>
        </div>
      </div>
      <div class="middle" v-html="item.content"></div>
      <div class="bottom">
        <span>点赞 {{ item.likeCount }}</span>
        <span>|</span>
        <span>浏览 {{ item.views }}</span>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "HmArticle",
  data() {
    return {
      list: [],
    };
  },
  created() {
    this.getList();
  },
  activated() {
    this.getList();
  },
  methods: {
    async getList() {
      const res = await axios({
        method: "get",
        url: "http://interview-api-t.itheima.net/h5/interview/query?pageSize=10",
        headers: {
          Authorization: `Bearer ${localStorage.getItem(`token`)}`,
        },
      });
      console.log(res);
      this.list = res.data.data.rows;
    },
  },
};
</script>

<style lang="less" scoped>
.item {
  padding: 10px;
  margin: 10px;
  border: 1px solid #ddd;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: transform 0.2s;
  .top {
    display: flex;

    img {
      width: 50px;
      height: 50px;
      border-radius: 50%;
    }

    .right {
      flex: 1;
    }
  }
  .middle {
    // 超过三行文字就省略
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
}
</style>
